﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Control;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using LPV_POS.Module;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraEditors.Repository;
using System.Reflection;

namespace LPV_POS.mh_pos
{
    public partial class mh_baocaodoanhthu_nguoigioithieu : DevExpress.XtraEditors.XtraForm
    {
        thuvien tv;
        DataTable tb;
        DataSet ds; //Dataset này dùng để nhận giá trị thay đổi khi thao tac

        public mh_baocaodoanhthu_nguoigioithieu()
        {
            InitializeComponent();
            InitControl();
            InitGrid();
            LoadDataGrid();
        }

        #region 1.InitControl

        private void InitControl()
        {
            f_showdataoncombox(CboKhachHang, f_selectdsNguoiGioiThieuchuforcom(), "OBJID", "OBJNAME");
            SetTuNgay_DenNgay(tungay, DenNgay);
            CboKhachHang._SetselectedID("-1");

        }

        // hien thi ds nhom chu len combobox
        private void f_showdataoncombox(AXCombobox cb, DataTable tb, string value, string display)
        {
            cb.Datasource = tb;
            cb.DisplayField = display;
            cb.ValueFiled = value;
            cb._Init();
        }

        #endregion

        #region 2.Khởi Tạo Lưới

        private void InitGrid()
        {
            Col_Ngay.FieldName = "NGAY_NHAP";
            Col_KhachHang.FieldName = "NGUOI_GIOI_THIEU_TEN";
            Col_SoPhieu.FieldName = "BH_PHIEU_MA";
            CotCal(Col_TONG_TIEN, "TONG_TIEN");
            CotCheckEdit(Col_Chon, "TINH_TRANG");
            Enable_GridColum(new GridColumn[] { Col_Ngay,Col_KhachHang,Col_TONG_TIEN,Col_SoPhieu});
        }

        private void LoadDataGrid()
        {
            ds = GetData(tungay.DateTime,DenNgay.DateTime,CboKhachHang._GetselectedID(),Duyet.Checked,KhongDuyet.Checked);
            try
            {
                //ds.Tables[0].Columns["THANH_TIEN"].Expression = "SO_LUONG * DON_GIA";
                GridControl_Data.DataSource = ds.Tables[0].DefaultView;
            }
            catch (Exception)
            {
                
            }
        }

        #endregion

        #region 3.Database

        // select danh sach Khách hàng
        private DataTable f_selectdsNguoiGioiThieuchuforcom()
        {
            tv = new thuvien();
            tb = tv.sql_SelectDataset("DM_KHACHHANG").Tables[0];
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            DataRow myNewRow;
            myNewRow = tb.NewRow();
            myNewRow["OBJID"] = "-1";
            myNewRow["OBJNAME"] = "--- Chọn khách hàng ---";
            tb.Rows.Add(myNewRow);
            //  tb.Rows.Add(n);
            tb.Columns.Add("STT", typeof(int));
            for (int i = 0; i < tb.Rows.Count; i++)
                tb.Rows[i]["STT"] = i + 1;

            //DataView dv_temp1 = tb.DefaultView;
            dv_temp.Sort = "STT DESC";
            return dv_temp.ToTable();
        }

        private DataSet GetData
           (
            DateTime TU_NGAY,
            DateTime DEN_NGAY,
            string ID_NGUOIGIOITHIEU,
            bool Duyet,bool KhongDuyet
           )
        {
            int flag = 0;
            if (Duyet == true)
                flag = 1;
            int flag2 = 0;
            if (KhongDuyet == true)
                flag2 = 1;
            string[] s;
            object[] o;
            s = new string[] { "@TU_NGAY", "@DEN_NGAY", "@NGUOI_GIOI_THIEU_MA", "@DUYET", "@KHONGDUYET" };
            o = new object[] { TU_NGAY, DEN_NGAY, ID_NGUOIGIOITHIEU, flag, flag2 };
            DataSet ds = tv.SP_SelectDataset("sp_BH_PHIEU_NGUOI_GIOI_THIEU_Select", s, o);
            return ds;
        }

        #endregion

        #region 4.Hỗ trợ

        /// <summary>
        /// Hàm gán giá trị cho từ ngày -> Ngày
        /// </summary>
        /// <param name="TuNgay">Control từ ngày</param>
        /// <param name="DenNgay">Control Đến ngày</param>
        private void SetTuNgay_DenNgay(DateEdit TuNgay, DateEdit DenNgay)
        {
            DenNgay.DateTime = DateTime.Now;
            TuNgay.DateTime = DateTime.Now.AddDays(-7);
        }

        private void CotCheckEdit(GridColumn Column, string FieldName)
        {
            RepositoryItemCheckEdit CheckEdit1 = new RepositoryItemCheckEdit();
            ((System.ComponentModel.ISupportInitialize)(CheckEdit1)).BeginInit();
            Column.ColumnEdit = CheckEdit1;
            CheckEdit1.Name = "CheckEdit";
            Column.FieldName = FieldName;
            ((System.ComponentModel.ISupportInitialize)(CheckEdit1)).EndInit();
        }

        private void Enable_GridColum(GridColumn[] Grid)
        {
            //Grid.OptionsColumn.AllowEdit = false;
            foreach (GridColumn item in Grid)
            {
                item.OptionsColumn.AllowEdit = false;
            }
            
        }

        public static void CotCal(GridColumn Column, string FieldName)
        {
            RepositoryItemCalcEdit Cal = new RepositoryItemCalcEdit();
            ((System.ComponentModel.ISupportInitialize)(Cal)).BeginInit();

            Cal.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
            new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)});

            Column.ColumnEdit = Cal;
            Column.FieldName = FieldName;
            Cal.Name = "Cal";
            Cal.DisplayFormat.FormatString = "N0";
            Cal.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            ((System.ComponentModel.ISupportInitialize)(Cal)).EndInit();
        }

        #endregion

        #region Xử lý nút

        private void btnTim_Click(object sender, EventArgs e)
        {
            LoadDataGrid();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            int i = tv.SP_inser_alltable("BH_PHIEU_NGUOI_GIOI_THIEU", ds.Tables[0]);
            if (i > 0)
            {
                MessageBox_LPV.Show("Lưu dữ liệu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox_LPV.Show("Lưu dữ liệu không thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BtnChoiceAll_Click(object sender, EventArgs e)
        {
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                if (r["TINH_TRANG"] != null)
                {
                    if (Convert.ToBoolean(r["TINH_TRANG"]) == false)
                    {
                        r["TINH_TRANG"] = true;
                    }
                }
            }
        }

        private void BtnChonKhachHang_Click(object sender, EventArgs e)
        {
            mh_dmkhachhang frm = new mh_dmkhachhang();
            DataRow dr = frm.ShowForm_fromBanHang("");
            if (dr != null)
            {
                if (dr["OBJID"].ToString() + "" != "")
                {
                    CboKhachHang._SetselectedID(dr["OBJID"].ToString());
                }
            }
        }

        private void BtnExportExcel_Click(object sender, EventArgs e)
        {
            cpublic c = new cpublic();
            c.ATExportExcel(GridView_Data);
        }

        #endregion

        
       
    }
}